<?php

class ReportOperationClass 
{
	public $_explicitType = "presstrack.ReportOperationClass";
    
	public $arMonthBySection;
	public $arOperation;
		
	static function GetOperationReport($year, $section)
	{
		set_time_limit(0);	
		$result->status = false;
		$report = new ReportOperationClass();
		
		// if  IN(3,5)
		if ($section != 4)
		{
			$sql = sprintf("SELECT label FROM press_track_new.operation WHERE section = $section");
			$query = mysql_query($sql);		
			if (mysql_errno())
			{
				$result->message = mysql_errno() . ": " . mysql_error();
				return $result;
			}		
			$ar_of_operation = array();
			while ($res = mysql_fetch_object($query)) 
			{
				$ar_of_operation[] = $res->label;
			}
		
			for ($i=1; $i < 13; $i++) 
			{
				$month = $i;
				if ($i < 10)
				{
					$month = "0" . $month;
				}	
			
				$ar_by_month = array();
				foreach ($ar_of_operation as $operation)
				{
					$sql = sprintf("SELECT SUM(cost) AS sum_of_operation FROM task "
						 . "WHERE task.label = '%s' "
						 . "AND task.order IN(SELECT idorder FROM press_track_new.order WHERE LOCATE('%s', press_track_new.order.creation, 3))", $operation, $month . "/" . $year);
					$query = mysql_query($sql);
					if (mysql_errno())
					{
						$result->message = mysql_errno() . ": " . mysql_error();
						return $result;
					}
					$res			= mysql_fetch_object($query);
					$row->name		= $operation;
					$row->first 	= 0;
					$row->second 	= 0;
					if ($res->sum_of_operation != null
					 && $res->sum_of_operation != 0)
					{
						$row->first = round($res->sum_of_operation, 2);
						$ar_by_month[] = clone $row;
					}
				}
				$report->arOperation[]	= $ar_by_month;
				$ar_by_month		 	= null;
				$array_of_orders		= null;
			}	
		}
		else if ($section == 4)
		{
			for ($i=1; $i < 13; $i++) 
			{
				$month = $i;
				if ($i < 10)
				{
					$month = "0" . $month;
				}
				$ar_by_month = array();
								
				$sql = sprintf("SELECT SUM(cost) AS sum_of_operation FROM task "
					 . "WHERE LOCATE('%s', value, 3) AND section = 4 "
					 . "AND task.order IN(SELECT idorder FROM press_track_new.order "
					 . "WHERE LOCATE('%s', press_track_new.order.creation, 3))", "SM-74", $month . "/" . $year);
				$query = mysql_query($sql);
				if (mysql_errno())
				{
					$result->message = mysql_errno() . ": " . mysql_error();
					return $result;
				}
				$res = mysql_fetch_object($query);
				$row->name		= "SM-74";
				$row->first 	= 0;
				$row->second 	= 0;
				if ($res->sum_of_operation != null
				 && $res->sum_of_operation != 0)
				{
					$row->first = round($res->sum_of_operation, 2);
					$ar_by_month[] = clone $row;
				}
				
				$sql = sprintf("SELECT SUM(cost) AS sum_of_operation FROM task "
					 . "WHERE LOCATE('%s', value, 3) AND section = 4 "
					 . "AND task.order IN(SELECT idorder FROM press_track_new.order "
					 . "WHERE LOCATE('%s', press_track_new.order.creation, 3))", "SM-52", $month . "/" . $year);
				$query = mysql_query($sql);
				if (mysql_errno())
				{
					$result->message = mysql_errno() . ": " . mysql_error();
					return $result;
				}
				$res = mysql_fetch_object($query);
				$row->name		= "SM-52";
				$row->first 	= 0;
				$row->second 	= 0;
				if ($res->sum_of_operation != null
				 && $res->sum_of_operation != 0)
				{
					$row->first = round($res->sum_of_operation, 2);
					$ar_by_month[] = clone $row;
					$row = null;
				}
				
				//
				$sql = sprintf("SELECT SUM(cost) AS sum_of_operation FROM task "
					 . "WHERE LOCATE('%s', value, 3) AND section = 4 "
					 . "AND task.order IN(SELECT idorder FROM press_track_new.order "
					 . "WHERE LOCATE('%s', press_track_new.order.creation, 3))", "Ryobi", $month . "/" . $year);
				$query = mysql_query($sql);
				if (mysql_errno())
				{
					$result->message = mysql_errno() . ": " . mysql_error();
					return $result;
				}
				$res = mysql_fetch_object($query);
				$row->name		= "Ryobi";
				$row->first 	= 0;
				$row->second 	= 0;
				if ($res->sum_of_operation != null
				 && $res->sum_of_operation != 0)
				{
					$row->first = round($res->sum_of_operation, 2);
					$ar_by_month[] = clone $row;
					$row = null;
				}
				
				$report->arOperation[]	= $ar_by_month;
				$ar_by_month		 	= null;
			}
		}
				
		$i = 1;
		foreach ($report->arOperation as $month)
		{
			$m = $i;
			if ($i < 10)
			{
				$m = "0" . $m;
			}
			$row2->name		= $m;
			$row2->first 	= 0;
			foreach ($month as $sum)
			{
				$row2->first = $row2->first + $sum->first;				
			}
			$report->arMonthBySection[] = clone $row2;
			$i++;
		}
		
		$result->status = true;
		$result->report = $report;
		return $result;
	}	
}
?>